package com.sgb.kjwl.viewmodel.machine;

import com.sgb.kjwl.api.APIManager;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.util.PickSelectEntity;
import com.sgb.kjwl.model.entity.ProListEntity;
import com.sgb.kjwl.model.entity.self.UserInquiryEntity;
import com.sgb.kjwl.repository.MachineRepertory;
import com.sgb.kjwl.view.ui.activity.WebViewX5HostActivity;
import com.sgb.kjwl.view.ui.fragment.self.MyReplyLeaseFragment;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MyReplyLeaseFragModel extends BaseViewModel {

    private MyReplyLeaseFragment leaseFragment;
    private MachineRepertory repertory;

    public MyReplyLeaseFragModel(MyReplyLeaseFragment leaseFragment, MachineRepertory repertory) {
        this.leaseFragment = leaseFragment;
        this.repertory = repertory;
    }

    //获取类型
    public void getFilterTypeModel() {
        APIManager.getInstance().getInquiryAPI().getInquiryType("").enqueue(new Callback<BaseEntity<List<PickSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<PickSelectEntity>>> call, Response<BaseEntity<List<PickSelectEntity>>> response) {
                if (APIManager.RESPONSE_OK.equals(response.body().getState())) {
                    leaseFragment.showClassTypeList(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<PickSelectEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });

    }


    public void getDataList(String classNo, String statusNo, int page) {
        Map map = new HashMap();
        map.put("pageNum", page);
        map.put("pageSize", 10);
        map.put("markType", classNo);//租赁类型1 设备 ；2 机械，3车辆； 4 房产，5土地，6 工厂 全部""
        map.put("state", statusNo); //全部"" 已回复5 未回复1
        DialogHelper.showProgressDialog(leaseFragment.getActivity(), null, "加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getInquiryAPI().getMyReplay(map).enqueue(new Callback<BaseEntity<ProListEntity<List<UserInquiryEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ProListEntity<List<UserInquiryEntity>>>> call, Response<BaseEntity<ProListEntity<List<UserInquiryEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (null != response.body().getData() && null != response.body().getData()) {
                        leaseFragment.showList(response.body().getData().getList());
                    } else {
                        MToast.showToast(leaseFragment.getActivity(), "暂无数据");
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ProListEntity<List<UserInquiryEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void getH5Url(String id) {
        DialogHelper.showProgressDialog(leaseFragment.getActivity(), null, "加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPickAPI().getUrlSetting("replyDetails").enqueue(new Callback<BaseEntity<H5ReEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<H5ReEntity>> call, Response<BaseEntity<H5ReEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    String url = response.body().getData().getUrl() + "?id=" + id;
                    WebViewX5HostActivity.start(leaseFragment.getActivity(), url);
                } else {
                    MToast.showToast(leaseFragment.getContext(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<H5ReEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }
}
